package com.jmu.Employee;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class TestStaff {

    private static void sortByName(Staff[] staffs) {
        /**
         * 思路：
         * 1： 新建一个 arraylist 把所有对象都加进去
         * 2： 根据年龄进行升序排序
         * 3： 赋值给原来的对象数组
         */

        //STEP 1
        ArrayList<Staff> result = NewArrayList(staffs);

        //STEP 2
        Collections.sort(result, new Comparator<Staff>() {
            @Override
            public int compare(Staff o1, Staff o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        //STEP 3
        ReBuildStaffs(staffs, result);

        //输出试试
        System.out.println("测试：按照姓名顺序升序排序");
        for (Staff t:staffs
             ) {
            System.out.print(t.getName() + "、");
        }
        System.out.println();
    }

    /**
     * 根据年龄进行排序
     */
    private static void sortByAge(Staff[] staffs) {
        /**
         * 思路：
         * 1： 新建一个 arraylist 把所有对象都加进去
         * 2： 根据年龄进行升序排序
         * 3： 赋值给原来的对象数组
         */

        //STEP 1
        ArrayList<Staff> result = NewArrayList(staffs);

        //STEP 2
        Collections.sort(result, new Comparator<Staff>() {
            @Override
            public int compare(Staff o1, Staff o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        //STEP 3
        ReBuildStaffs(staffs, result);

        //输出试试
        System.out.println("测试：按照年龄升序排序");
        for (Staff t:staffs
        ) {
            System.out.print(t.getAge() + "、");
        }
        System.out.println();
    }

    /**
     * 支持对Staff类及其子类 按照各自的薪水 进行 降序排序
     */
    private static void sortBySalary(Staff[] staffs) {
        /**
        初步想法：
        1: 新建一个 arraylist 把对象数组中的对象全都放进去 并且把各种奖金整合到总薪水里头去
        2: 根据薪水的高低进行降序排序
        3: 把这个 arraylist 赋值给原来的对象数组
         */

        //STEP 1
        ArrayList<Staff> result = new ArrayList<>();
        for (Staff p:staffs
             ) {
            if (p instanceof Dean) {
                p.setTotalSalary(p.getSalary() + ((Dean) p).getAdminAward());
            } else if (p instanceof SecurityGuard) {
                p.setTotalSalary(p.getSalary() + ((SecurityGuard) p).getDangerousAllowance());
            } else if (p instanceof Teacher) {
                p.setTotalSalary(p.getSalary() + ((Teacher) p).getPostAllowance());
            } else {
                p.setTotalSalary(p.getSalary());
            }
            result.add(p);
        }

        //STEP 2
        Collections.sort(result, new Comparator<Staff>() {
            @Override
            public int compare(Staff o1, Staff o2) {
                return o2.getTotalSalary() - o1.getTotalSalary();
            }
        });

        //STEP 3
        ReBuildStaffs(staffs, result);

        //输出试试
        System.out.println("测试：按照薪水降序排序");
        for (Staff t:staffs
        ) {
            System.out.print(t.getTotalSalary() + "、");
        }
        System.out.println();
    }

    /**
     * 把排序好的对象数组重新赋值给原来的对象数组
     * @param staffs
     * @param result
     */
    private static void ReBuildStaffs(Staff[] staffs, ArrayList<Staff> result) {
        for (int i = 0; i < staffs.length; i++) {
            staffs[i] = result.get(i);
        }
    }

    /**
     * 新建一个 arralist 把所有对象都加进去
     * @param staff
     * @return ArrayList
     */
    private static ArrayList NewArrayList(Staff[] staff) {
        ArrayList<Staff> result = new ArrayList<>();
        for (Staff p:staff
        ) {
            result.add(p);
        }
        return result;
    }

    /**
     * 打印出每个人的名字
     */
    private static void printName(Staff[] persons) {
        System.out.println("这里有以下员工（原始顺序）：");
        for (Staff p:persons
             ) {
            System.out.print(p.getName() + " ");
        }
        System.out.println();
    }

    /**
     * 打印出薪水
     */
    private static void printSalary(Staff[] staffs) {
        System.out.println("输出员工的工资：");
        for (Staff p:staffs
             ) {
            if ((p instanceof Staff) && !(p instanceof Teacher) && !(p instanceof Dean) && !(p instanceof SecurityGuard))
                System.out.println(p.getName() + " " + p.getSalary());
            else if (p instanceof Teacher) {
                Teacher teacher = (Teacher)p;
                System.out.println(teacher.getName() + " " +(teacher.getSalary() + teacher.getPostAllowance()));
            } else if (p instanceof SecurityGuard) {
                SecurityGuard securityGuard = (SecurityGuard)p;
                System.out.println(securityGuard.getName() + " " + (securityGuard.getSalary() + securityGuard.getDangerousAllowance()));
            } else {
                Dean dean = (Dean)p;
                System.out.println(dean.getName() + " " + (dean.getSalary() + dean.getAdminAward()));
            }
        }
    }

    public static void main(String[] args) {
        //添加若干个Staff, Teacher, SecurityGuard, Dean实例
        Staff staff = new Staff("A","AXXX",18,"boy",1500,new Date(2016,11,12));
        Staff staff1 = new Staff("E", "EXXX", 21, "woman", 2300, new Date(2017,1,8));
        Staff teacher = new Teacher("B","BXXX",32,"man",4500,new Date(1994,6,8),"Computer","soft",1200);
        Staff securityGuard = new SecurityGuard("C","CXXX",26,"man",2000,new Date(2008,10,1),"kill",1500);
        Staff dean = new Dean("D","DXXX",52,"man",6000,new Date(2015,9,8),3000);

        Staff[] employee = {staff1, teacher, securityGuard, dean, staff};

        printName(employee);
        printSalary(employee);
        sortBySalary(employee);
        sortByAge(employee);
        sortByName(employee);
    }
}
